package ma.ensao.evalmetrics.interpretation;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import ma.ensao.evalmetrics.controller.MethodManager;
import ma.ensao.evalmetrics.model.MethodInterp;
import ma.ensao.evalmetrics.model.ResultMethod;

import com.opensymphony.xwork2.ActionSupport;

public class MethodIntManager extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Long idp;
	private MethodManager methodmanager;
	private List<ResultMethod> resultmethod;
	private List<MethodInterp> records;
	private MethodInterp record;
	private String result;
	private int TotalRecordCount,jtStartIndex,jtPageSize;
	String jtSorting;

	//data from post to create new method 
	
	private Long  id;
	private int idtest;
	private Long idProjet;
	private String Classe;
	private String type;
	private String namemethode;
	private float Vg;
	private int Sloc;
	private Date date;

	public String afficher() {
		
		records = new ArrayList<MethodInterp>() ;
		methodmanager = new MethodManager();
		System.out.println("start"+jtStartIndex+"size"+jtPageSize);
		this.resultmethod=methodmanager.listAll(idp,jtStartIndex,jtPageSize,jtSorting);
		MethodInterp myResult;
		for(ResultMethod res : resultmethod){
			myResult = map(res);
			records.add(myResult);
		}
		result="OK";
		TotalRecordCount = methodmanager.countTotal(idp);

		return SUCCESS;
	}
	
public String ajouter() {
		record = new MethodInterp() ;
		ResultMethod method = new ResultMethod();
		method.setClasse(Classe);
		method.setDate(new Date());
		method.setIdProjet(idp);
		method.setNamemethode(namemethode);
		method.setSloc(Sloc);
		method.setType(type);
		method.setVg(Vg);

		methodmanager = new MethodManager();
		method = methodmanager.addMethod(method);
		
		result="OK";
		record = map(method);
		
		return SUCCESS;
	}

	public String update() {
		
		ResultMethod method = new ResultMethod();

		method.setClasse(Classe);
		method.setDate(new Date());
		method.setIdProjet(idp);
		method.setNamemethode(namemethode);
		method.setSloc(Sloc);
		method.setType(type);
		method.setVg(Vg);
		method.setId(id);
		method.setIdtest(idtest);
		
		methodmanager = new MethodManager();
		methodmanager.updateMethod(method);
		
		result="OK";
		
		return SUCCESS;
	}
	
	public String delete() {
		
		ResultMethod method = new ResultMethod();

		method.setId(id);
		System.out.println("id"+id);
		
		methodmanager = new MethodManager();
		methodmanager.deleteMethod(method);
		
		result="OK";
		
		return SUCCESS;
	}
	
	private MethodInterp map(ResultMethod result){
		MethodInterp myResult = new MethodInterp();
		
		myResult.setClasse(result.getClasse());
		myResult.setDate(result.getDate());
		myResult.setId(result.getId());
		myResult.setIdProjet(result.getIdProjet());
		myResult.setNamemethode(result.getNamemethode());
		myResult.setSloc(result.getSloc());
		myResult.setType(result.getType());
		myResult.setVg(result.getVg());
		myResult.setIdtest(result.getIdtest());
		
		double vgInt;
		if(result.getSloc()>30){
			vgInt = Math.pow(2,(6-result.getVg())/3);
		}
		else{
			vgInt = 3;
		}
		vgInt=new BigDecimal(vgInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		myResult.setVgInt(vgInt);
		
		double slocInt;
		if(result.getSloc()>70){
			slocInt=Math.pow(2,(70-result.getSloc())/21);
		}
		else{
			slocInt = 3;
		}
		slocInt = new BigDecimal(slocInt ).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
		myResult.setSlocInt(slocInt);
		
		return myResult;
	}

	public Long getIdp() {
		return idp;
	}

	public void setIdp(Long idp) {
		this.idp = idp;
	}

	public List<MethodInterp> getRecords() {
		return records;
	}

	public void setRecords(List<MethodInterp> records) {
		this.records = records;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public MethodInterp getRecord() {
		return record;
	}

	public void setRecord(MethodInterp record) {
		this.record = record;
	}

	public int getIdtest() {
		return idtest;
	}

	public void setIdtest(int idtest) {
		this.idtest = idtest;
	}

	public Long getIdProjet() {
		return idProjet;
	}

	public void setIdProjet(Long idProjet) {
		this.idProjet = idProjet;
	}

	public String getClasse() {
		return Classe;
	}

	public void setClasse(String classe) {
		Classe = classe;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNamemethode() {
		return namemethode;
	}

	public void setNamemethode(String namemethode) {
		this.namemethode = namemethode;
	}

	public float getVg() {
		return Vg;
	}

	public void setVg(float vg) {
		Vg = vg;
	}

	public int getSloc() {
		return Sloc;
	}

	public void setSloc(int sloc) {
		Sloc = sloc;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getTotalRecordCount() {
		return TotalRecordCount;
	}

	public void setTotalRecordCount(int totalRecordCount) {
		TotalRecordCount = totalRecordCount;
	}

	public int getJtStartIndex() {
		return jtStartIndex;
	}

	public void setJtStartIndex(int jtStartIndex) {
		this.jtStartIndex = jtStartIndex;
	}

	public int getJtPageSize() {
		return jtPageSize;
	}

	public void setJtPageSize(int jtPageSize) {
		this.jtPageSize = jtPageSize;
	}

	public String getJtSorting() {
		return jtSorting;
	}

	public void setJtSorting(String jtSorting) {
		this.jtSorting = jtSorting;
	}

	

	
}
